package com.wyg.demo.juc.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.wyg.demo.juc.properties.ThreadPoolConfigProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author WenYanguang
 * @since 2023/3/19
 */
@Configuration
public class MyThreadConfig {
    @Value("${demo.thread.core-size}")
    private Integer coreSize;
    
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties properties) {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("wyg-demo-pool-%d").build();
        return new ThreadPoolExecutor(coreSize, 
                                properties.getMaxSize(), 
                                properties.getKeepAliveTime(), 
                                TimeUnit.SECONDS,
                                new LinkedBlockingDeque<>(100000),
                                threadFactory,
                                new ThreadPoolExecutor.AbortPolicy());
    }
}
